package com.opgea.tutor.app;

import java.util.Calendar;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.criterion.Restrictions;

import com.opgea.tutor.domain.entities.Login;
import com.opgea.tutor.domain.entities.Student;
import com.opgea.tutor.domain.entities.Tutor;
import com.opgea.tutor.domain.qualifiers.UserType;

public class HibernateApp {

    public static SessionFactory getSessionFactory() {
	Configuration config = new Configuration();
	config.configure("hibernate.cfg.xml");
	SessionFactory sessionFactory = config.buildSessionFactory();
	return sessionFactory;
    }

    public void saveStudent() {
	Session session = getSessionFactory().openSession();
	try {
	    session.beginTransaction();
	    Student student = new Student();
	    student.setFirstName("SHEKHAR");
	    student.setMiddleName("KUMAR");
	    student.setLastName("GUPTA");
	    student.setSex('M');
	    student.setTitle("MR");
	    Calendar cal = Calendar.getInstance();
	    cal.set(1981 - 2000, 01, 15);
	    java.util.Date dob = cal.getTime();
	    student.setDateOfBirth(dob);
	    session.save(student);
	    session.getTransaction().commit();
	} catch (HibernateException ex) {
	    if (session.getTransaction() != null) {
		session.getTransaction().rollback();
	    }
	} finally {
	    session.close();
	}
    }

    public void findStudent() {
	Session session = getSessionFactory().openSession();
	session.beginTransaction();
	Query query = session
		.createQuery("from Student s WHERE s.firstName = 'SHEKHAR'");
	// Query query = session.createSQLQuery("SELECT * FROM STUDENT");
	@SuppressWarnings("unchecked")
	List<Student> students = query.list();
	for (Student student : students) {
	    System.out.println(student);
	}
    }

    public void createLogin() {
	Session session = getSessionFactory().openSession();
	session.beginTransaction();
	Login login = new Login();
	Tutor tutor = new Tutor();
	login.setLoginId("shekhar@gmail.com");
	login.setPassword("shekhar");
	login.setUserType(UserType.Tutor);
	login.setIsActive(true);
	session.save(login);
	tutor.setLogin(login);
	session.save(tutor);
	session.getTransaction().commit();
    }

    public void findAllLogins() {
	Session session = getSessionFactory().openSession();
	session.beginTransaction();
	Query query = session.createQuery("from Login");
	@SuppressWarnings("unchecked")
	List<Login> logins = query.list();
	for (Login login : logins) {
	    System.out.println(login);
	}
    }

    public void findTutor() {
	Session session = getSessionFactory().openSession();
	session.beginTransaction();
	Query query = session.createQuery("from Tutor t WHERE t.login.id =:id");
	query.setParameter("id", 1L);
	@SuppressWarnings("unchecked")
	List<Tutor> tutors = query.list();
	for (Tutor tutor : tutors) {
	    System.out.println(tutor);
	}
    }

    public Tutor findTutorByCriteria(long id) {
	@SuppressWarnings("unchecked")
	List<Tutor> objects = getSessionFactory().openSession()
		.createCriteria(Tutor.class).add(Restrictions.eq("id", id))
		.list();
	System.out.println(objects.get(0));
	return objects.get(0);
    }

    public static void main(String[] args) {
	HibernateApp app = new HibernateApp();
	// app.saveStudent();
	// app.findStudent();
	app.createLogin();
	// app.findAllLogins();
	// app.findTutor();
	// app.findTutorByCriteria(11);
    }

}
